package com.example.controller;


import com.example.model.WxbGood;
import com.example.model.util.PageUtils;
import com.example.model.vo.Result;
import com.example.impl.WxbGoodsServiceImpl;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/goods")
public class WxbGoodsController {

    @Autowired
    private WxbGoodsServiceImpl wxbGoodsService;

//
//    @RequestMapping("goods/upload")
//    @ResponseBody
//    public HashMap<String,Object>upload(MultipartFile file){
//
//        HashMap<String,Object>map = new HashMap<>();
//        try{
//            String fileName = System.currentTimeMillis()+file.getOriginalFilename();
//            FTPClient ftpClient =new FTPClient();
//            ftpClient.connect("192.168.238.1",21);
//            boolean state = ftpClient.login("anonymus","851523232@qq.com");
//            int replyCode =ftpClient.getReplyCode();
//            if (FTPReply.isPositiveCompletion(replyCode)){
//                ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
//                ftpClient.makeDirectory("img");
//                ftpClient.changeWorkingDirectory("img");
//                InputStream fileInputStream = file.getInputStream();
//                String fn = new String(fileName.getBytes("UTf-8"),"iso-8859-1");
//                ftpClient.storeFile(fn,fileInputStream);
//                ftpClient.logout();
//                System.out.println("finsh..");
//                map.put("code",1);
//                map.put("msg","http://192.168.238.1.81/img/"+fileName);
//            }
//        }catch(IOException e){
//            e.printStackTrace();
//        }
//        return map;
//    }
//
//    @RequestMapping("goods/add")
//    @ResponseBody
//    public JsonResultVo add(@RequestBody WxbGood goods){
//
//        JsonResultVo vo =null;
//        try {
//            wxbGoodsService.addGoodS(goods);
//            vo = new JsonResultVo(1,"success");
//        }catch (Exception e){
//            vo=new JsonResultVo(0,"fail");
//        }
//        return null;
//    }



    @RequestMapping("/saveGood")
    public String saveGood(WxbGood wxbGood,Model model, MultipartFile pic1, MultipartFile pic2, MultipartFile pic3) {

        System.out.println(wxbGood + "------------商品");
        System.out.println(pic1 + "------------图片");
        try {
            String fileName1 = System.currentTimeMillis() + pic1.getOriginalFilename();
            String fileName2 = System.currentTimeMillis() + pic2.getOriginalFilename();
            String fileName3 = System.currentTimeMillis() + pic3.getOriginalFilename();

            FTPClient ftpClient = new FTPClient();
            ftpClient.connect("192.168.54.11", 21);
            boolean state = ftpClient.login("anonymous", "381985027@qq.com");
            int replyCode = ftpClient.getReplyCode();
            if (FTPReply.isPositiveCompletion(replyCode)) {
                ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
                ftpClient.makeDirectory("images");
                ftpClient.changeWorkingDirectory("images");

                InputStream fileInputStream1 = pic1.getInputStream();
                InputStream fileInputStream2 = pic2.getInputStream();
                InputStream fileInputStream3 = pic3.getInputStream();
                String fn1 = new String(fileName1.getBytes("UTF-8"), "iso-8859-1");
                String fn2 = new String(fileName2.getBytes("UTF-8"), "iso-8859-1");
                String fn3 = new String(fileName3.getBytes("UTF-8"), "iso-8859-1");
                ftpClient.storeFile(fn1, fileInputStream1);
                ftpClient.storeFile(fn2, fileInputStream2);
                ftpClient.storeFile(fn3, fileInputStream3);
                ftpClient.logout();
                wxbGood.setGoodPic("http://192.168.54.11:88/images/" + fn1);
                wxbGood.setGoodPic1("http://192.168.54.11:88/images/" + fn2);
                wxbGood.setGoodPic2("http://192.168.54.11:88/images/" + fn3);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


        boolean result = wxbGoodsService.saveGood(wxbGood);
        if (result) {
            return "goods-list";
        } else {
            return "goods-add";
        }

    }

    @RequestMapping("/insert")
    public String insertWxfGoods() {


        return "goods-add";
    }

    @RequestMapping("/list")
    @ResponseBody
    public PageUtils list(Integer pageNum){
        PageUtils page = wxbGoodsService.list(pageNum);
        return page;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Result delete(@RequestBody HashMap<String,Object> map) {

        List<String> goodsId = (List<String>) map.get("goodId");

        try {
            wxbGoodsService.deleteByGoodId(goodsId);
          return new Result(true,"操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,"参数不足,操作失败");
        }

    }
}
